package xyz.yolin.service;

import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import xyz.yolin.mapper.AnswerOptMapper;
import xyz.yolin.mapper.AnswerTxtMapper;
import xyz.yolin.mapper.SurveyMapper;
import xyz.yolin.pojo.AnswerOpt;
import xyz.yolin.pojo.AnswerTxt;
import xyz.yolin.pojo.Survey;
import xyz.yolin.utils.BeanMapUtils;
import xyz.yolin.utils.MapParameter;

import java.util.List;
import java.util.Map;

@Service
public class SurveyServiceImpl implements SurveyService {
    @Autowired
    private SurveyMapper surveyMapper;
    @Autowired
    private AnswerOptMapper answerOptMapper;
    @Autowired
    private AnswerTxtMapper answerTxtMapper;


    @Override
    public int create(Survey survey) {
        return surveyMapper.create(survey);
    }

    @Override
    public int delete(String str) {
        List<String> list = Splitter.on(",").splitToList(str);
        int flag=0;
        for(String i:list){
            surveyMapper.delete(MapParameter.getInstance().putId(Integer.parseInt(i)).getMap());
            flag++;
        }
        return flag;
    }

    @Override
    public int update(Survey survey) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(survey))
                .putId(survey.getId()).getMap();
        return surveyMapper.update(map);
    }

    @Override
    public List<Survey> query(Survey survey) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(survey)).getMap();
        return surveyMapper.query(map);
    }

    @Override
    public Survey detail(int id) {
        Map<String, Object> map = MapParameter.getInstance().putId(id).getMap();
        return surveyMapper.detail(map);
    }

    @Override
    public int count(Survey survey) {
        return surveyMapper.count(MapParameter.getInstance().put(BeanMapUtils.beanToMap(survey)).getMap());
    }
    @Override
    public void updateState(){
        List<Integer> list = surveyMapper.queryByState(Survey.state_create);
        for (Integer id:list){
            surveyMapper.update(MapParameter.getInstance().put("updateState",Survey.state_exec).put("id",id).getMap());
        }
        List<Integer> list2 = surveyMapper.queryByStateForExec(Survey.state_exec);
        for (Integer id:list2){
            surveyMapper.update(MapParameter.getInstance().put("updateState",Survey.state_over).put("id",id).getMap());
        }
    }

    @Override
    public List<Survey> queryAll(Survey survey) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(survey)).getMap();
        return surveyMapper.query(map);
    }

    @Override
    public Integer submit(List<AnswerOpt> opts, List<AnswerTxt> txts) {
        int flag=0;
        for (AnswerOpt opt:opts){
            answerOptMapper.create(opt);
        }
        for (AnswerTxt txt:txts){
            flag=answerTxtMapper.create(txt);
        }
        return flag;
    }

    @Override
    public List<AnswerOpt> queryAnswerOpt(AnswerOpt answerOpt) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(answerOpt)).getMap();
        return answerOptMapper.query(map);
    }


}
